package cc.booku.owlbox.Statistics;

import android.graphics.Color;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.R;
import cc.booku.owlbox.RsaUtil.Decrypt;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class EqdetailActivity extends AppCompatActivity {
    private Toolbar sToolbar;
    private String ProfilePath = Environment.getExternalStorageDirectory() + File.separator + "OwlDatabase" + File.separator + "Profile.txt";
    private List<eq_detail_C> eq_detail_cList = new ArrayList<eq_detail_C>();
    private ListView list_eqdetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eqdetail);
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));
        list_eqdetail = (ListView) findViewById(R.id.list_eqdetail);

        //拿到点击的设备
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String Device = bundle.getString("Device");

        sToolbar = (Toolbar) findViewById(R.id.toolbar);
        sToolbar.setTitle(Device + "用户");
        setSupportActionBar(sToolbar);//利用Toolbar代替ActionBar
        sToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        httpdata(Device);

    }

    //请求数据
    private void httpdata(String Device) {
        Message msg = new Message();
        msg.what = 12;
        Bundle bundle = new Bundle();
        bundle.putString("Device", Device);  //往Bundle中存放数据
        msg.setData(bundle);//mes利用Bundle传递数据
        handler.sendMessage(msg);//用activity中的handler发送消息

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case 12:
                    String str9 = msg.getData().getString("Device");//接受msg传递过来的参数
                    if (deviceInformation() != null) {
                        HttpResponse(deviceInformation() + str9);
                    } else Log.d("Thandler--12", "地址错误======" + deviceInformation());
                    break;
                case 16:
                    listendeviceData((ArrayList<eq_detail_C>) eq_detail_cList);
                    break;


            }
        }
    };


    private eq_detail_C eqDetailC;

    private void HttpResponse(String url) {

        eq_detail_cList.clear();

        OkHttpClient client = new OkHttpClient();
        Request request = new Request
                .Builder()
                .url(url)//要访问的链接
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                List<eq_detail_C> collectionStationCList = JSON.parseArray(res, eq_detail_C.class);
                for (eq_detail_C normVideo : collectionStationCList) {
                    eqDetailC = new eq_detail_C();


                    String vod_date = normVideo.getVod_date();
                    String vod_device = normVideo.getVod_device();
                    String vod_ip = normVideo.getVod_ip();
                    String vod_model = normVideo.getVod_model();
                    String vod_username = normVideo.getVod_username();
                    String vod_version = normVideo.getVod_version();

                    eqDetailC.setVod_date(vod_date);
                    eqDetailC.setVod_device(vod_device);
                    eqDetailC.setVod_ip("ip：" + vod_ip);
                    eqDetailC.setVod_model(vod_model);
                    eqDetailC.setVod_username(vod_username);
                    eqDetailC.setVod_version("V：" + vod_version);

                    eq_detail_cList.add(eqDetailC);


                }
                Message msg = new Message();
                msg.what = 16;
                handler.sendMessage(msg);//用activity中的handler发送消息
            }
        });

    }

    private eq_detail_A eqDetailA;

    public void listendeviceData(ArrayList<eq_detail_C> eq_detail_cList) {


        eqDetailA = new eq_detail_A(R.layout.eqdetail_item, this, eq_detail_cList);

        list_eqdetail.setAdapter(eqDetailA);
        list_eqdetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                //跳转提示
                Toast.makeText(EqdetailActivity.this, "Click item" + i, Toast.LENGTH_SHORT).show();
                //final TextView vod_class1 = (TextView) view.findViewById(R.id.vod_class);

            }
        });


    }

    private String deviceInformation() {

        String ConfigFiles_json = FileoOperations.readAssetsTxt(EqdetailActivity.this, "TxtData/ConfigFiles");
        JSONObject objsd = JSONObject.parseObject(ConfigFiles_json);
        String ciphertext = objsd.getString("ADDRESSCIPHER");//地址密文
        String DecryptionAddress = Decrypt.RsaDecrypt(EqdetailActivity.this, ciphertext);
        String deviceinfo = objsd.getString("DEVICEINFO");//类型检索

        return DecryptionAddress + deviceinfo;
//        //读取sd文件的json
//        String ProfileStr = FileoOperations.readFromSDFile(this, ProfilePath);
//        //读取配置文件信息
//        try {
//            JSONObject obj = JSONObject.parseObject(ProfileStr);
//            String result = obj.getString("用户设备信息");
//            String ciphertext = obj.getString("地址密文");
//            String res = Decrypt.RsaDecrypt(EqdetailActivity.this,ciphertext);
//
//
//
//            return res+result;
//
//
//        } catch (Exception e) {
//
//            Toast.makeText(EqdetailActivity.this, "配置文件错误", Toast.LENGTH_SHORT).show();
//
//
////异常处理
//        }

        //  return null;
    }

}
